package com.example.admin.midtermpro41;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;
/*这是一个抽象类，减少recycleView具体实现写的重复代码*/
abstract class QuickAdapter<T> extends RecyclerView.Adapter<QuickAdapter.VH>{
    private List<T> mDatas;
    OnItemClickListener onItemClickListener;
    public QuickAdapter(List<T> datas){
        this.mDatas = datas;
    }

    public abstract int getLayoutId(int viewType);

    @Override
    public VH onCreateViewHolder(ViewGroup parent, int viewType) {
        return VH.get(parent,getLayoutId(viewType));
    }

    @Override
    public void onBindViewHolder(final VH holder, int position) {
        convert(holder, mDatas.get(position), position);
        if (onItemClickListener != null) {
            for(int i=0;i<holder.mViews.size();i++){
                holder.itemView.findViewById(holder.mViews.valueAt(i).getId()).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //Log.i("debug"," "+view.getId());
                        onItemClickListener.onClick(holder.getAdapterPosition(),view.getId());
                        //给recyclerView中每个组件增加点击事件,记住组件要在convert里面set过这里才能得到
                    }
                });
            }
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onItemClickListener.onClick(holder.getAdapterPosition(),view.getId());
                }
            });
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    onItemClickListener.onLongClick(holder.getAdapterPosition(),view.getId());
                    return false;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }
    public List<T> getItem(){
        return mDatas;
    }
    public interface OnItemClickListener{
        void onClick(int position,int viewId);
        void onLongClick(int position,int viewId);
    }

    public void setOnItemClickListener(OnItemClickListener _onItemClickListener) {
        this.onItemClickListener = _onItemClickListener;
    }

    public abstract void convert(VH holder, T data, int position);

    static class VH extends RecyclerView.ViewHolder{
        private SparseArray<View> mViews;
        private View mConvertView;

        private VH(View v){
            super(v);
            mConvertView = v;
            mViews = new SparseArray<>();
        }

        public static VH get(ViewGroup parent, int layoutId){
            View convertView = LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false);
            return new VH(convertView);
        }

        public <T extends View> T getView(int id){
            View v = mViews.get(id);
            if(v == null){
                v = mConvertView.findViewById(id);
                mViews.put(id, v);
            }
            return (T)v;
        }

        public void setText(int id, String value){
            TextView view = getView(id);
            view.setText(value);
        }
        public void setImage(int imageID,int newImageID){
            ImageView imageView=getView(imageID);
            imageView.setBackgroundResource(newImageID);
        }
        public void setViewVisibility(int viewID,int visibility){
            TextView textView=getView(viewID);
            textView.setVisibility(visibility);
        }

    }
}